import { rand } from "@vueuse/shared"
import store from "."
import router from "../route"

class RouterData {

    constructor() {
        this.data_list = new Array()
        this.get_max_count = 3
    }

    push(name, old_data=null, new_data=null, router_params={}) {    
        // console.log('name', name, 'old_data', old_data, 'new_data', new_data)
        if (old_data)
            store.commit('show','backTab') // @RISK 
            this.data_list.push(old_data)
        // 进入新页面
        console.log('new_data', new_data, old_data,this.data_list)
        if(new_data)
            this.data_list.push(new_data) // 存入新数据
        // 改变
        let push_data = {
            params: router_params
        }
        if (name.startsWith('/')){
            push_data.path = name
        }else{
            push_data.name = name
        }
        
        router.push({
            ...push_data,
            query: rand(1,100000)
        })
        console.log('push后的',this.data_list, this.data_list.length)

    }

    clear(){
        store.commit('hidden','backTab')//@RISK 
        this.data_list = new Array()
    }

    async get() {
        if (this.data_list.length == 0) {
            let sleep = (time) => new Promise((resolve) => {
                setTimeout(resolve, time)
            })
            await sleep(500)
            if (this.get_max_count == 0){
                console.log('达到了最大的次数')
                // store.commit('changeStage', {name: 'course'})
                store.commit('hidden','backTab')
                return
            }
            this.get_max_count -= 1
            return this.get()
        } else if (this.data_list.length == 1 && this.data_list[0] && this.data_list[0].length == 0){ // 也就是没有获取到数据，[]空数组的情况
            this.data_list.pop() // 清空
            console.log('进入')
            return
        } else {
            let data = this.data_list.pop()
            if(this.data_list.length == 0)
                store.commit('hidden','backTab') // 说明回到了最顶页
            this.get_max_count = 5 // 重置次数
            return data
        }
    }
}


export {
    RouterData
}